package com.xiaoxing.zhinenghuacangku_linyi.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;
import com.xiaoxing.zhinenghuacangku_linyi.R;
import com.xiaoxing.zhinenghuacangku_linyi.R2;
import com.xiaoxing.zhinenghuacangku_linyi.di.component.DaggerLinYi_KaiGuanMenComponent;
import com.xiaoxing.zhinenghuacangku_linyi.di.module.LinYi_KaiGuanMenModule;
import com.xiaoxing.zhinenghuacangku_linyi.mvp.contract.LinYi_KaiGuanMenContract;
import com.xiaoxing.zhinenghuacangku_linyi.mvp.presenter.LinYi_KaiGuanMenPresenter;
import com.xiaoxing.zhinenghuacangku_linyi.mvp.ui.entity.LinYiKaiGuanMen;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.ZHI_NENG_HUA_CANG_KU_LIN_YI_ACTIVITY_LIN_YI_KAI_GUAN_MEN)
public class LinYi_KaiGuanMenActivity extends BaseActivity<LinYi_KaiGuanMenPresenter> implements LinYi_KaiGuanMenContract.View {


    @BindView(R2.id.et_qu_huo_ma)
    EditText etQuHuoMa;
    @BindView(R2.id.tv_kai_men)
    TextView tvKaiMen;
    @BindView(R2.id.tv_guan_men)
    TextView tvGuanMen;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerLinYi_KaiGuanMenComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .linYi_KaiGuanMenModule(new LinYi_KaiGuanMenModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.zhi_neng_cang_ku_lin_yi_activity_lin_yi_kai_guan_men_lin_yi; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.zhi_neng_cang_ku_lin_yi_activity_lin_yi_kai_guan_men));
//        getLinYiKaiGuanMenData();
    }

    private void getLinYiKaiGuanMenData() {
        HashMap<String, String> map = new HashMap<>();

        mPresenter.getLinYiKaiGuanMenData(map);
    }

    @Override
    public void getLinYiKaiGuanMenDataSuccess(LinYiKaiGuanMen entityData) {


    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick({R2.id.tv_kai_men, R2.id.tv_guan_men})
    public void onViewClicked(View view) {
        int id = view.getId();

        if (id == R.id.tv_kai_men) {
            String quHuoMa = etQuHuoMa.getText().toString().trim();
            if (TextUtils.isEmpty(quHuoMa)) {
                showMessage("请输入取货码");
                return;
            }
            openOutStoreDoor(quHuoMa);
        } else if (id == R.id.tv_guan_men) {
            closeOutStoreDoor();
        }
    }

    private void openOutStoreDoor(String vcode) {


        String methodName = "OpenOutStoreDoor";
        SoapParams params = new SoapParams();
        params.put("vcode", vcode);

        /**
         * 请求
         */
        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                LoadingDialogUtil.dismissDialog();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {

                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                LoadingDialogUtil.dismissDialog();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                LoadingDialogUtil.dismissDialog();
            }
        }, getAapiUserName(), getApiPwd());
    }

    private void closeOutStoreDoor() {


        String methodName = "CloseOutStoreDoor";
        SoapParams params = new SoapParams();

        /**
         * 请求
         */
        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                LoadingDialogUtil.dismissDialog();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {

                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                LoadingDialogUtil.dismissDialog();
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                LoadingDialogUtil.dismissDialog();
            }
        }, getAapiUserName(), getApiPwd());
    }
}
